/*
  Name: DeadProxySocketInfoHandler.h
  Copyright:
  Author: Carl Chatfield
  Date: 18/01/08 17:30
  Description: Used for matching SOCKETs to client numbers
*/


#include "DeadProxySocketInfoHandler.h"
#include "DeadProxyApp.h"
#include <wx/arrimpl.cpp> //this is needed for wxArrays, i dont know why lol
WX_DEFINE_OBJARRAY(ArrayOfSocketInfos);

SocketInfoHandler::SocketInfoHandler()
{
    SocketInfos = new ArrayOfSocketInfos;
    mutex = new wxMutex();
    mutex->Unlock();
}

void SocketInfoHandler::AddSocketInfo(SOCKET s, int c)
{
    mutex->Lock();
    SocketInfo * NewSocketInfo = new SocketInfo();
    NewSocketInfo->ClientNumber = c;
    NewSocketInfo->Socket = s;
    SocketInfos->Add(NewSocketInfo);
    mutex->Unlock();
}

void SocketInfoHandler::RemoveSocketInfo(SOCKET s)
{
    mutex->Lock();
    int len = SocketInfos->GetCount();
    for(int i = 0; i < len; i++)
    {
        if(SocketInfos->Item(i).Socket == s)
        {
            SocketInfos->RemoveAt(i);
            break;
        }
    }
    mutex->Unlock();
}

int SocketInfoHandler::GetClient(SOCKET s)
{
    int ret = -1;
    mutex->Lock();
    int len = SocketInfos->GetCount();
    for(int i = 0; i < len; i++)
    {
        if(SocketInfos->Item(i).Socket == s)
        {
            ret = SocketInfos->Item(i).ClientNumber;
            break;
        }
    }
    mutex->Unlock();
    return ret;
}
